package com.example.demo.jucConcurrent.ThreadLocal详解;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 如果用线程来操作ThreadLocal对象确实会造成内存泄漏
 */
public class ThreadLocalDemo {

    static  class  LocalVariable{
        private Long[] a = new  Long[1024 * 1024];
    }
    final  static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.MINUTES,
            new LinkedBlockingQueue<>());

    final  static  ThreadLocal<LocalVariable> localVariable= new ThreadLocal<LocalVariable>();


    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(5000*4);

        for (int i = 0; i <50 ; i++) {

            poolExecutor.execute(()->{

                localVariable.set(new LocalVariable());
                // (5)
                System.out.println("use local varaible" + localVariable.get());
                localVariable.remove();
            });
        }
        System.out.println("pool executor over");
    }

}

